package com.zzyl.service.impl;

import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    /*
     * 根据房间id获得床位
     */
    
    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public int createBed(BedDto bedDto) {
        Long count = bedMapper.countDiffBedNumber(bedDto.getBedNumber(), bedDto.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_REPEAT_BED_NUMBER);
        }
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto, bed);
        bed.setBedStatus(0);
        bedMapper.updateBedNum(bed);
        return bedMapper.createBed(bed);
    }

    @Override
    public BedVo getBed(Long id) {
        BedVo bed = bedMapper.getBedById(id);
        return bed;
    }

    @Override
    public int updateBed(BedDto bedDto) {
        Long count = bedMapper.countDiffBedNumber(bedDto.getBedNumber(), bedDto.getId());
        if (count > 0) {
            throw new BaseException(BasicEnum.BED_INSERT_FAIL_REPEAT_BED_NUMBER);
        }
        BedDto bedStatue = bedMapper.getBedStaueById(bedDto.getId());
        if (bedStatue.getBedStatus().equals(1)) {
            throw new BaseException(BasicEnum.BED_EDIT_FAIL_INUSE);
        }
        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto, bed);
        return bedMapper.updateBed(bed);
    }

    @Override
    public int deleteBed(Long id) {
        bedMapper.updateBedNumMin(id);
        return bedMapper.deleteBed(id);
    }
}

